<?

if(!defined(SCOT_WP_SOURCE)) {
	define('SCOT_WP_SOURCE', true);
	
	class WPSource extends Source {
		function getUserList() {
			if(SCOT_DEBUG) echo('Call : CiteULikeSource->getUserList()<br>');
			
			$dbc = $this->getDB();
			
			$this->userList = array();

			$userCount = $dbc->query("SELECT ID FROM wp_users;");

			for($i = 0 ; $i < $userCount ; $i++) {
				$this->userList[] = $dbc->get_var(null, 0, $i);
			}

			return $this->userList;
		}
		
		function getUserPostList($userid) {
			if(SCOT_DEBUG) echo('Call : CiteULikeSource->getUserPostList()<br>');
			
			$dbc = $this->getDB();
			
			$this->userPostList = array();
			
			$userPostCount = $dbc->query("SELECT ID FROM wp_posts WHERE post_author = '$userid';");
			
			for($i = 0 ; $i < $userPostCount ; $i++) {
				$this->userPostList[] = $dbc->get_var(null, 0, $i);
			}
			
			return $this->userPostList;
		}
		
		function getTagPostList($tagid) {
			if(SCOT_DEBUG) echo('Call : CiteULikeSource->getTagPostList()<br>');
			
			$dbc = $this->getDB();
			
			$this->tagPostList = array();
	
			$tagPostCount = $dbc->query("SELECT post_id FROM wp_post2cat where category_id = '$tagid' ORDER BY post_id;");

			for($i = 0 ; $i < $tagPostCount ; $i++) {
				$this->tagPostList[] = $dbc->get_var(null, 0, $i);
			}
			
			return $this->tagPostList;
		}
		
		function getPostTagList($postid) {
			if(SCOT_DEBUG) echo('Call : CiteULikeSource->getPostTagList()<br>');
			
			$dbc = $this->getDB();
			
			$this->postTagList = array();
	
			$postTagCount = $dbc->query("SELECT category_id FROM wp_post2cat where post_id = '$postid' ORDER BY category_id;");

			for($i = 0 ; $i < $postTagCount ; $i++) {
				$this->postTagList[] = $dbc->get_var(null, 0, $i);
			}
			
			return $this->postTagList;			
		} 
		
		function getInfo($infoName, $id) {
			$info = null;
			
			$dbc = $this->getDB();
			
			switch($infoName) {
				case SCOT_SOURCE_BLOG_NAME : {
					$info = $dbc->get_var("SELECT option_value FROM wp_options WHERE option_name = 'blogname'", 0, 0);
										break;
				}
				case SCOT_SOURCE_SITE_URL : {
					$info = $dbc->get_var("SELECT option_value FROM wp_options WHERE option_name = 'siteurl'", 0, 0);
					break;
				}
				case SCOT_SOURCE_TAGCLOUD_ID : {
//					$info = $dbc->get_var("SELECT user_nicename FROM wp_users WHERE ID = '$id'", 0, 0);
					$info = "/".$id;//.date('YmdHis',time());
					break;
				}
				case SCOT_SOURCE_USER_NAME : {
					$info = $dbc->get_var("SELECT user_nicename FROM wp_users WHERE ID = '$id'", 0, 0);
					break;
				}
				case SCOT_SOURCE_POST_TITLE : {
					$info = $dbc->get_var("SELECT post_title FROM wp_posts WHERE ID = '$id'", 0, 0);
					break;
				}
				case SCOT_SOURCE_POST_DATE : {
					$info = $dbc->get_var("SELECT post_date FROM wp_posts where ID = '$id'", 0, 0);
					break;
				}
				case SCOT_SOURCE_TAG_NAME : {
					$info = $dbc->get_var("SELECT cat_name FROM wp_categories WHERE cat_ID = '$id'", 0, 0);
					break;
				}
				default : {
					echo('Exception : Invalid InfoName Exception : getUserInfo('.$infoName.')<br>');
				}
			}
			return $info;
		}
		
		function getDateFormat() {
			return 'Y-m-d h:i:s';
		}
	}
}
?>